package com.lvgang.springbootrabbitmq.topic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lvgang
 */
@Configuration
public class RabbitTopicConfig {

    private static Logger logger = LoggerFactory.getLogger(RabbitTopicConfig.class);

    /**
     * Queue 可以有4个参数
     *      1.队列名
     *      2.durable       持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
     *      3.auto-delete   表示消息队列没有在使用时将被自动删除 默认是false
     *      4.exclusive     表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean
    public Queue createTopicQueueA() {
        logger.info("创建TopicQueueA成功");
        return new Queue(RabbitTopicConstant.QUEUQ_A,true);
    }
    @Bean
    public Queue createTopicQueueB() {
        logger.info("创建TopicQueueB成功");
        return new Queue(RabbitTopicConstant.QUEUQ_B,true);
    }

    @Bean
    public TopicExchange topicExchange() {
        //配置广播路由器
        logger.info("创建TopicExchange成功");
        return new TopicExchange(RabbitTopicConstant.EXCHANGE);

    }

    @Bean
    public Binding bingQueueAToTopicExchange() {
        logger.info("绑定TopicQueueA到TopicExchange成功");
        return BindingBuilder.bind(createTopicQueueA()).to(topicExchange()).with(RabbitTopicConstant.QUEUQ_KEY_A);
    }

    @Bean
    public Binding bingQueueBToTopicExchange() {
        logger.info("绑定TopicQueueB到TopicExchange成功");
        return BindingBuilder.bind(createTopicQueueB()).to(topicExchange()).with(RabbitTopicConstant.QUEUQ_KEY_B);
    }

}
